<template>
	<view style="padding: 20px;">
		<view>
			<tetx>接收人手机号<text style="color: red;">*</text></tetx>
			<u-input style="margin-top: 10px;" v-model="phonenumber"></u-input>
		</view>
		<view>
			<tetx>转让原码数量<text style="color: red;">*</text></tetx>
			<u-input style="margin-top: 10px;" v-model="totalCodeNumber"></u-input>
		</view>
		<view style="margin-top: 20px;">
			<tetx>剩余数量<text style="color: red;"></text></tetx>
			<text style="margin-left: 20px;color: red;">{{totalVailbleCodeNumber}}</text>
		</view>
		<u-button type="primary"  style="margin-top: 20px;margin-bottom: 20%;" @click="sumbit">提交</u-button>
		<u-loading-icon text="提交中"  :show="showLoading" textSize="18" ></u-loading-icon>
	</view>
	
</template>

<script>
	import {listOriginalQRcode,transferOriginalQRcode} from "@/api/system/originalQRcode.js"
	export default {
		data() {
			return {
				user: this.$store.state.user,
				phonenumber:"",
				totalCodeNumber:"",
				totalVailbleCodeNumber:"",
				showLoading:false,
			}
		},
		onReady() {
			this.getTotalVailbleOriginalCount()
		},
		
		methods: {
			sumbit(){
				if(this.phonenumber == ""){
						uni.showToast({
							title: "请输入接收人手机号",
							icon: "none"
						})
						return
					}
					const phoneReg = /^1[3-9]\d{9}$/;  // 中国大陆手机号正则表达式
					
					if (!phoneReg.test(this.phonenumber)) {
					    uni.showToast({
					        title: "请输入正确的手机号",
					        icon: "none"
					    });
					    return;
					}
					if(this.totalCodeNumber === "" || !/^[1-9]\d*$/.test(this.totalCodeNumber)) {
											
					    uni.showToast({
					        title: "请输入有效的转让原码数量",
					        icon: "none"
					    })
					    return
					}

					if(this.totalVailbleCodeNumber < this.totalCodeNumber){
					
						uni.showToast({
						    title: "剩余数量不足",
						    icon: "none"
						})
						return
					}
					this.showLoading = true
					
					transferOriginalQRcode({
						phonenumber:this.phonenumber,
						totalCodeNumber:this.totalCodeNumber,
						
					}).then(response => {
						this.showLoading = false
						uni.showToast({
						  title: '提交成功',
						  icon: 'success'
						});
						setTimeout(() => {
						  uni.navigateTo({
						    url: '/pages/home/admin/originalQR/OriginalQRManager',
						  });
						}, 600);
					}).catch(error => {
						this.showLoading = false
						
					})
				
			},
			getTotalVailbleOriginalCount(){
				listOriginalQRcode({
			
					pageNum: 1,
					pageSize: 1000,
					phonenumber:this.user.phonenumber,
					state: "1"
				}).then(response => {
					this.totalVailbleCodeNumber = response.total
				})
			},
			
		}
	}
</script>

<style>

</style>
